class FASTAParser {
  Map currentSequence;
  Function seqCallback;

  FASTAParser(Function seqCallback) {
    this.seqCallback = seqCallback;
  }

  addLine(String line) {
    var defMatch = RegExp('^>\s*(\S+)\s*(.*)').allMatches(line);

    if (defMatch == null || defMatch.length == 0) {
      return;
    }

    RegExpMatch _match = defMatch.first;
    this._flush();
    this.currentSequence = {
      'id': _match.group(1),
      'sequence': '',
      'description': _match.groupCount > 2 ? _match.group(2).trim() : null,
    };
    if (this.currentSequence != null && RegExp('\S').hasMatch(line)) {
      this.currentSequence['sequence'] += line.replaceAll(RegExp('\s'), '');
    }
  }

  _flush() {
    if (this.currentSequence != null) this.seqCallback(this.currentSequence);
  }

  finish() {
    this._flush();
  }
}
